//供应商代码
var artChanged = false;
var manChanged = false;
var houseWaresChanged = false;
var selectRowPurchase;
//获取供应商代码option
function getSupplierComboGridOptions() {
    var newOptions = {
        textField: 'memberNo',
        columns: [[{
            field: 'id',
            title: '编号',
            width: 150,
            checkbox: true
        }, {
            field: 'memberNo',
            title: '供应商代码',
            width: 100
        }, {
            field: 'memberName',
            title: '供应商名称',
            width: 100
        }, {
            field: 'abbrName',
            title: '供应商简称',
            width: 150
        }]],
        keyHandler: {
            up: function () {
            },
            down: function (q) {
            },
            enter: function (q) {
                comboSearch(q);
            },
            query: function (q) {
                comboSearch(q);
            }
        },
        onChange: function (newValue, oldValue) {
            artChanged = true;//记录是否有改变（当手动输入时发生)
        },
        onSelect: function (index, row) {
            selectRowPurchase = row;
        },
        onHidePanel: function () {
            var t = $(this).combogrid('getValue');
            if (artChanged) {
                if (selectRowPurchase == null || t != selectRowPurchase.id) {//没有选择或者选项不相等时清除内容
                    alert('请选择供应商代码，不要直接输入');
                    $(this).combogrid('setValue', '');
                } else {
                    $('#providerName').val(selectRowPurchase.memberName);
                }
            }
            artChanged = false;
            selectRowPurchase = null;
        }
    }
    return newOptions;
}

//获取付款类型
function getPayTypeOptions() {
    var payTypeOptions = {
        valueField: 'payName',
        textField: 'payName',
        idField:'payName',
        columns: [[{
            hidden:'true',
            field: 'id',
            title: '编号',
            width: 150
        }, {
            field: 'payCode',
            title: '付款类型代码',
            width: 100
        }, {
            field: 'payName',
            title: '付款类型名称',
            width: 100
        }]]
    }
    return payTypeOptions;
}

//获取票据种类
function getNoteTypeByFIdOptions() {
    var noteTypeByFIdOptions = {
        valueField: 'noteName',
        textField: 'noteName',
        idField: 'noteName',
        columns: [[{
            hidden:'true',
            field: 'id',
            title: '编号',
            width: 150
        }, {
            field: 'noteCode',
            title: '票据类型代码',
            width: 100
        }, {
            field: 'noteName',
            title: '票据类型名称',
            width: 100
        }]]
    }
    return noteTypeByFIdOptions;
}

//获取银行信息
function getBankInfoOptions() {
    var bankInfoOptions = {
        valueField: 'bankName',
        textField: 'bankName',
        idField: 'bankName',
        columns: [[{
            hidden:'true',
            field: 'id',
            title: '编号',
            width: 150
        }, {
            field: 'company',
            title: '银行代码',
            width: 100
        }, {
            field: 'bankName',
            title: '银行名称',
            width: 100
        }]]
    }
    return bankInfoOptions;
}

//获取签约人options
function userComboGridOptions() {
    var userNewOptions = {
        textField: 'trueName',
        columns: [[{
            field: 'id',
            title: '编号',
            width: 150,
            checkbox: true
        }, {
            field: 'userNo',
            title: '员工工号',
            width: 100
        }, {
            field: 'trueName',
            title: '员工姓名',
            width: 100
        }, {
            field: 'deptName',
            title: '所属部门',
            width: 150
        }]],
        //keyHandler: {
        //    up: function () {
        //    },
        //    down: function () {
        //    },
        //    enter: function (q) {
        //        var options = $('#memberNo').combogrid("options");
        //        q = $('#memberNo').combo('getText');
        //        options.keyword = q;
        //        initComboDataGrid("/sysUserController/selectUser.html", $("#memberNo"), options);
        //        $('#memberNo').combogrid("setValue", q);
        //    },
        //    query: function (q) {
        //        var options = $('#memberNo').combogrid("options");
        //        q = $('#memberNo').combo('getText');
        //        options.keyword = q;
        //        initComboDataGrid("/sysUserController/selectUser.html", $("#memberNo"), options);
        //        $('#memberNo').combogrid("setValue", q);
        //    }
        //},
        onChange: function (newValue, oldValue) {
            manChanged = true;//记录是否有改变（当手动输入时发生)
        },
        onSelect: function (index, row) {
            selectRowPurchase = row;
        },
        onHidePanel: function () {
            var t = $(this).combogrid('getValue');
            if (manChanged) {
                if (selectRowPurchase == null || t != selectRowPurchase.id) {//没有选择或者选项不相等时清除内容
                    alert('签约人，不要直接输入，请选择');
                    $(this).combogrid('setValue', '');
                } else {
                    $('#signDept').val(selectRowPurchase.deptName);
                }
            }
            manChanged = false;
            selectRowPurchase = null;
        }
    }
    return userNewOptions;
}


//获取仓库options
function houseWaresOptions() {
    var houseWaresOptions = {
        textField: 'warehouseCode',
        columns: [[{
            field: 'warehouseCode',
            title: '仓库代码',
            width: 100
        }, {
            field: 'warehouseName',
            title: '仓库名称',
            width: 100
        }]],
        //keyHandler: {
        //    up: function () {
        //    },
        //    down: function () {
        //    },
        //    enter: function (q) {
        //        var options = $('#memberNo').combogrid("options");
        //        q = $('#memberNo').combo('getText');
        //        options.keyword = q;
        //        initComboDataGrid("/sysUserController/selectUser.html", $("#memberNo"), options);
        //        $('#memberNo').combogrid("setValue", q);
        //    },
        //    query: function (q) {
        //        var options = $('#memberNo').combogrid("options");
        //        q = $('#memberNo').combo('getText');
        //        options.keyword = q;
        //        initComboDataGrid("/sysUserController/selectUser.html", $("#memberNo"), options);
        //        $('#memberNo').combogrid("setValue", q);
        //    }
        //},
        onChange: function (newValue, oldValue) {
            houseWaresChanged = true;//记录是否有改变（当手动输入时发生)
        },
        onSelect: function (index, row) {
            selectRowPurchase = row;
        },
        onHidePanel: function () {
            var t = $(this).combogrid('getValue');
            if (houseWaresChanged) {
                if (selectRowPurchase == null || t != selectRowPurchase.id) {//没有选择或者选项不相等时清除内容
                    alert('仓库代码，不要直接输入，请选择');
                    $(this).combogrid('setValue', '');
                } else {
                    $('#warehouseName').val(selectRowPurchase.warehouseName);
                }
            }
            houseWaresChanged = false;
            selectRowPurchase = null;
        }
    }
    return houseWaresOptions;
}



//获取采购申请列表的options
function getApplicationDataGridOptions(options) {
    var initOptions = {
        idField: 'applicationId',
        sortName: 'applicationId',
        sortOrder: 'asc',
        columns: [[{
            title: '编号',
            field: 'ID',
            width: 150,
            checkbox: true
        }, {
            title: '采购申请号',
            field: 'applicationNumber',
            width: 150
        }, {
            field: 'status',
            title: '申请状态',
            width: 150
        }, {
            field: 'providerName',
            title: '供应商名称',
            width: 150
        }, {
            field: 'taxGoodsAmount',
            title: '含税货款金额',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'taxAmount',
            title: '税额',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'totalAmount',
            title: '总金额',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'totalWeight',
            title: '总重量',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatWeight(value);
            }
        }, {
            field: 'providerContract',
            title: '供应商合同号',
            width: 150
        }, {
            field: 'contractDate',
            title: '签约日期',
            width: 150
        }, {
            field: 'userName',
            title: '签约人',
            width: 150
        }, {
            field: 'remark',
            title: '备注',
            width: 150
        }]]
    }
    if (options != undefined) {
        $.extend(initOptions, options);
    }
    return initOptions;
}

//获取无需编辑的采购申请子项的options（审批页面）
function getNotEditSubOptions() {
    var initOptions = {
        idField: 'cid',
        fitColumns: false,
        width: 1600,
        checkOnSelect: false,
        singleSelect: false,
        fit: false,
        columns: [[
            {
                field: 'subApplicationNumber',
                title: '采购申请子项号',
                width: 110
            }, {
                title: '子项状态',
                field: 'statusSub',
                width: 60
            }, {
                field: 'weight',
                title: '*重量',
                width: 80,
                align:'right',
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    return formatWeight(value);
                }
            }, {
                field: 'unitPrice',
                title: '*单价(含税)',
                width: 80,
                align:'right',
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    return formatNum(value);
                }
            }, {
                field: 'paymentAmount',
                title: '货款金额',
                width: 80,
                align:'right',
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    return formatNum(value);
                }
            }, {
                field: 'goodsName',
                title: '*品名',
                width: 60
            }, {
                field: 'marks',
                title: '*牌号',
                width: 60
            }, {
                field: 'cpspecification',
                title: '*规格',
                width: 110
            }, {
                field: 'cdName',
                title: '*产地',
                width: 60
            }, {
                field: 'standards',
                title: '技术标准',
                width: 60
            }, {
                field: 'qualityGrade',
                title: '质量等级',
                width: 60
            }, {
                field: 'unitWeight',
                title: '件重',
                width: 60
            }, {
                field: 'totalCost',
                title: '金额总计',
                width: 80,
                align:'right',
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    return formatNum(value);
                }
            }, {
                field: 'deliveryMethod',
                title: '*交货方式',
                width: 60
            },
            {
                field: 'subtaxAmount',
                width: 80,
                title: '税额',
                align:'right',
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    return formatNum(value);
                }
            },
            {
                field: 'taxRate',
                width: 40,
                title: '税率',
                align:'right',
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    return formatNum(value);
                }
            }, {
                field: 'qualityNum',
                title: '质保书份数',
                width: 70
            }, {
                field: 'appSubContract',
                title: '供应商合同子项号',
                width: 110
            }, {
                field: 'resourceNumber',
                title: '资源号',
                width: 60
            }, {
                field: 'remark',
                title: '备注',
                width: 60
            }]]
    }
    return initOptions;
}

function getNotEditPaymentApSubOptions() {
    var initOptions = {
        idField: 'cid',
        fitColumns: false,
        checkOnSelect: false,
        singleSelect: false,
        fit: false,
        columns: [[{
            hidden:'true',
            title: '采购合同号id',
            field: 'f_contractId',
            width: 150
        },{
            title: '采购合同号',
            field: 'contractCode',
            width: 100
        }, {
            title: '含税货款金额',
            field: 'taxAmount',
            width: 100,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'totalAmount',
            title: '总金额',
            width: 100,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }

        }, {
            field: 'appBalance',
            title: '*申请金额',
            width: 100,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        },  {
            field: 'paidAmount',
            title: '实付金额',
            width: 100,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        },
            {
                field: 'privoderCcode',
                title: '供应商合同号',
                width:110
            }, {
                field: 'moneyType',
                title: '款项类型',
                width: 100,
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    for (var i = 0; i < payMethodJson.length; i++) {
                        if (payMethodJson[i].id == value) {
                            return payMethodJson[i].orName;
                        }
                    }
                }
            }, {
                field: 'paymentDate',
                title: '付款基准日',
                width: 100,
                align:'right'
            },{
                field: 'remark',
                title: '备注',
                width: 100
            }, {
                field: 'padAppSub',
                title: '付款申请子项号',
                width: 100
            }]]
    }
    return initOptions;
}

//获取合同列表的options
//采购合同号、合同状态、供应商名称、含税货款金额、税额、总金额、已付款金额、申请中金额、总重量、合同性质、供应商合同号、签约日期、签约人、备注。
function getContractListDataFridOptions(options) {
    var initOptions = {
        idField: 'contractCode',
        sortName: 'contractCode',
        sortOrder: 'asc',
        columns: [[{
            width: 150,
            checkbox: true
        }, {
            title: '采购合同号',
            field: 'contractCode',
            width: 150
        }, {
            field: 'contractStatus',
            title: '合同状态',
            width: 150
        }, {
            field: 'providerName',
            title: '供应商名称',
            width: 150
        }, {
            field: 'taxGoodsAmount',
            title: '含税货款金额',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'taxAmount',
            title: '税额',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'totalAmount',
            title: '总金额',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'amountPaid',
            title: '已付款金额',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'amountApplication',
            title: '申请中金额',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(value);
            }
        }, {
            field: 'totalWeight',
            title: '总重量',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatWeight(value);
            }
        }, {
            field: 'contractNature',
            title: '合同性质',
            width: 150
        }, {
            field: 'providerContract',
            title: '供应商合同号',
            width: 150
        }, {
            field: 'contractDate',
            title: '签约日期',
            width: 150
        }, {
            field: 'userName',
            title: '签约人',
            width: 150
        }, {
            field: 'remark',
            title: '备注',
            width: 150
        }]]
    }
    if (options != undefined) {
        $.extend(initOptions, options);
    }
    return initOptions;
}

function getPaymentContractInfo(options){
    var initOptions = {
        idField: 'contractCode',
        sortName: 'contractCode',
        sortOrder: 'asc',
        checkOnSelect: true,
        singleSelect:false,
        selectOnCheck:true,
        columns: [[{
            width: 150,
            checkbox: true
        },
            {
                hidden:'true',
            title: '采购合同号id',
            field: 'id',
            width: 150
        },
            {
            title: '采购合同号',
            field: 'contractCode',
            width: 150
        }, {
            field: 'taxGoodsAmount',
            title: '含税货款金额',
            width: 150,
                align:'right',
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    return formatNum(value);
                }
        }, {
            field: 'amountPaid',
            title: '已付款金额',
            width: 150,
                align:'right',
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    return formatNum(value);
                }
        }, {
            field: 'amountApplication',
            title: '申请中金额',
            width: 150,
                align:'right',
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    return formatNum(value);
                }
        }, {
            field: 'totalToPayAmount',
            title: '总金额',
            width: 150,
            align:'right',
            formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                return formatNum(rowData.taxGoodsAmount - rowData.amountPaid);
            }
        }, {
            field: 'totalWeight',
            title: '总重量',
            width: 150,
                align:'right',
                formatter: function (value, rowData, rowIndex) {//格式化该字段内的显示
                    return formatWeight(value);
                }
        },{
            field: 'contractDate',
            title: '签约日期',
            width: 150
        }, {
            field: 'providerContract',
            title: '供应商合同号',
            width: 150
        },{
            field: 'contractStatus',
            title: '合同状态',
            width: 150
        }]]
    }
    if (options != undefined) {
        $.extend(initOptions, options);
    }
    return initOptions;
}


